<template>
  <el-dialog
    :title="type==2 ? '新增季后赛/总决赛数据' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="场均得分" prop="score">
      <el-input v-model="dataForm.score" placeholder="场均得分"></el-input>
    </el-form-item>
    <el-form-item label="场均篮板" prop="rebounds">
      <el-input v-model="dataForm.rebounds" placeholder="场均篮板"></el-input>
    </el-form-item>
    <el-form-item label="场均助攻" prop="assists">
      <el-input v-model="dataForm.assists" placeholder="场均助攻"></el-input>
    </el-form-item>
    <el-form-item label="场均抢断" prop="steals">
      <el-input v-model="dataForm.steals" placeholder="场均抢断"></el-input>
    </el-form-item>
    <el-form-item label="场均盖帽" prop="blockedShots">
      <el-input v-model="dataForm.blockedShots" placeholder="场均盖帽"></el-input>
    </el-form-item>
    <el-form-item label="出场时间" prop="playingTime">
      <el-input v-model="dataForm.playingTime" placeholder="出场时间"></el-input>
    </el-form-item>
    <el-form-item label="出场数" prop="appearances">
      <el-input v-model="dataForm.appearances" placeholder="出场数"></el-input>
    </el-form-item>
    <el-form-item label="场均犯规" prop="foul">
      <el-input v-model="dataForm.foul" placeholder="场均犯规"></el-input>
    </el-form-item>
    <el-form-item label="场均失误" prop="fault">
      <el-input v-model="dataForm.fault" placeholder="场均失误"></el-input>
    </el-form-item>
    <el-form-item label="场均投篮数" prop="shotNumber">
      <el-input v-model="dataForm.shotNumber" placeholder="场均投篮数"></el-input>
    </el-form-item>
    <el-form-item label="投篮命中率(%)" prop="shotPercentage">
      <el-input v-model="dataForm.shotPercentage" placeholder="投篮命中率"></el-input>
    </el-form-item>
    <el-form-item label="三分命中率(%)" prop="threePercentage">
      <el-input v-model="dataForm.threePercentage" placeholder="三分命中率"></el-input>
    </el-form-item>
    <el-form-item label="三分命中数" prop="threeNumber">
      <el-input v-model="dataForm.threeNumber" placeholder="三分命中数"></el-input>
    </el-form-item>
    <el-form-item label="罚球数" prop="freeshotNumber">
      <el-input v-model="dataForm.freeshotNumber" placeholder="罚球数"></el-input>
    </el-form-item>
    <el-form-item label="赛季" prop="season">
      <el-input v-model="dataForm.season" placeholder="赛季"></el-input>
    </el-form-item>
    <el-form-item label="罚球命中率(%)" prop="freeshotPercentage">
      <el-input v-model="dataForm.freeshotPercentage" placeholder="罚球命中率"></el-input>
    </el-form-item>
    <el-form-item label="赛程" prop="stage">
      <el-input v-model="dataForm.stage" placeholder="常规赛(0)/季后赛(1)/总决赛(2)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        type:'',
        dataForm: {
          id: 0,
          playerId: '',
          score: '',
          rebounds: '',
          assists: '',
          steals: '',
          blockedShots: '',
          playingTime: '',
          appearances: '',
          foul: '',
          fault: '',
          shotNumber: '',
          shotPercentage: '',
          threePercentage: '',
          threeNumber: '',
          freeshotNumber: '',
          season: '',
          freeshotPercentage: '',
          stage: ''
        },
        dataRule: {
          playerId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          score: [
            { required: true, message: '场均得分不能为空', trigger: 'blur' }
          ],
          rebounds: [
            { required: true, message: '场均篮板不能为空', trigger: 'blur' }
          ],
          assists: [
            { required: true, message: '场均助攻不能为空', trigger: 'blur' }
          ],
          steals: [
            { required: true, message: '场均抢断不能为空', trigger: 'blur' }
          ],
          blockedShots: [
            { required: true, message: '场均盖帽不能为空', trigger: 'blur' }
          ],
          playingTime: [
            { required: true, message: '出场时间不能为空', trigger: 'blur' }
          ],
          appearances: [
            { required: true, message: '出场数不能为空', trigger: 'blur' }
          ],
          foul: [
            { required: true, message: '场均犯规不能为空', trigger: 'blur' }
          ],
          fault: [
            { required: true, message: '场均失误不能为空', trigger: 'blur' }
          ],
          shotNumber: [
            { required: true, message: '场均投篮数不能为空', trigger: 'blur' }
          ],
          shotPercentage: [
            { required: true, message: '投篮命中率不能为空', trigger: 'blur' }
          ],
          threePercentage: [
            { required: true, message: '三分命中率不能为空', trigger: 'blur' }
          ],
          threeNumber: [
            { required: true, message: '三分命中数不能为空', trigger: 'blur' }
          ],
          freeshotNumber: [
            { required: true, message: '罚球数不能为空', trigger: 'blur' }
          ],
          season: [
            { required: true, message: '赛季不能为空', trigger: 'blur' }
          ],
          freeshotPercentage: [
            { required: true, message: '罚球命中率不能为空', trigger: 'blur' }
          ],
          stage: [
            { required: true, message: '常规赛/季后赛/总决赛不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,type) {
        this.type = type
        if(type == 1){
        this.dataForm.id = id 
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/player/playerbasicdate/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.playerId = data.playerBasicdate.playerId
                this.dataForm.score = data.playerBasicdate.score
                this.dataForm.rebounds = data.playerBasicdate.rebounds
                this.dataForm.assists = data.playerBasicdate.assists
                this.dataForm.steals = data.playerBasicdate.steals
                this.dataForm.blockedShots = data.playerBasicdate.blockedShots
                this.dataForm.playingTime = data.playerBasicdate.playingTime
                this.dataForm.appearances = data.playerBasicdate.appearances
                this.dataForm.foul = data.playerBasicdate.foul
                this.dataForm.fault = data.playerBasicdate.fault
                this.dataForm.shotNumber = data.playerBasicdate.shotNumber
                this.dataForm.shotPercentage = data.playerBasicdate.shotPercentage
                this.dataForm.threePercentage = data.playerBasicdate.threePercentage
                this.dataForm.threeNumber = data.playerBasicdate.threeNumber
                this.dataForm.freeshotNumber = data.playerBasicdate.freeshotNumber
                this.dataForm.season = data.playerBasicdate.season
                this.dataForm.freeshotPercentage = data.playerBasicdate.freeshotPercentage
                this.dataForm.stage = data.playerBasicdate.stage
              }
            })
          }
        })
        }
        else{
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
          this.$http({
          url: this.$http.adornUrl(`/player/playerbasicdate/getPlayerId/${id}`),
          method: 'get',
        }).then(({data}) => {
          this.dataForm.playerId = data.playerId
        })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/player/playerbasicdate/${this.type==2 ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'playerId': this.dataForm.playerId,
                'score': this.dataForm.score,
                'rebounds': this.dataForm.rebounds,
                'assists': this.dataForm.assists,
                'steals': this.dataForm.steals,
                'blockedShots': this.dataForm.blockedShots,
                'playingTime': this.dataForm.playingTime,
                'appearances': this.dataForm.appearances,
                'foul': this.dataForm.foul,
                'fault': this.dataForm.fault,
                'shotNumber': this.dataForm.shotNumber,
                'shotPercentage': this.dataForm.shotPercentage,
                'threePercentage': this.dataForm.threePercentage,
                'threeNumber': this.dataForm.threeNumber,
                'freeshotNumber': this.dataForm.freeshotNumber,
                'season': this.dataForm.season,
                'freeshotPercentage': this.dataForm.freeshotPercentage,
                'stage': this.dataForm.stage
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
